package dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import dao.interf.IAuthorityDAO;
import dao.pojo.Authority;
import dao.pojo.User;

public class AuthorityDAO extends BaseDAO<Authority> implements IAuthorityDAO{

	public List<Authority> findAuthorityByUser(final User user)
			throws RuntimeException {
		Session sess = this.getSessionFactory().openSession();
		String sqlQuery = "SELECT m1.authority FROM GroupAuthority m1, GroupUser m2" +
				" WHERE m2.user=:user AND m1.group=m2.group";
		Query query = null;
		List<Authority> lst = new ArrayList<Authority> (0);
		try {
			query = sess.createQuery(sqlQuery);
			query.setParameter("user", user);
			lst = query.list();
		} finally {
			if(null != sess) {
				sess.close();
				sess = null;
			}
		}
		return lst;
	}
}
